package com.javaclimb.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaclimb.mappers.TreeMapper;
import com.javaclimb.pojo.SelectTree;
import com.javaclimb.utils.PageBean;
import com.javaclimb.pojo.Tree;
import com.javaclimb.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TreeServiceImp implements TreeService {

    @Autowired
    private TreeMapper treeMapper;

    @Override
    public List<Tree> getAllTrees() {
        return treeMapper.selectAllTree();
    }

    /*根据市/区查询古树信息*/
    public PageBean page(Integer page, Integer pageSize, SelectTree st) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        String city = "";
        String district= "";
        if(st.getSelectedAddresses().length==2){
            city = st.getSelectedAddresses()[1];
            district = st.getSelectedAddresses()[1];
        }
        if(st.getSelectedAddresses().length==3){
            city = st.getSelectedAddresses()[1];
            district = st.getSelectedAddresses()[2];
        }
        //2.执行查询
        List<Tree> treeList = treeMapper.list(city,district,
                st.getLevel(),st.getTreeType(),st.getGrowthStatus(),st.getCode());//传递条件查询参数
        Page<Tree> p = (Page<Tree>) treeList;
        //3.封装bean对象
        PageBean pB = new PageBean(p.getTotal(), p.getResult());
        return pB;
    }

    @Override
    public void delete(List<Integer> ids) {
        treeMapper.delete02(ids);//删除顺序不能换
        treeMapper.delete01(ids);
    }

    @Override
    public void save(Tree tree) {
        //先对tree对象属性city，district进行封装
        tree_cd(tree);
        treeMapper.insert01(tree);
        treeMapper.insert02(tree);
    }

    @Override
    public Tree getById(Integer id) {
        Tree tree01 = treeMapper.getId01(id);
        Tree tree02 = treeMapper.getId02(id);
        return merge(tree01,tree02);
    }

    @Override
    public void update(Tree tree) {
        tree_cd(tree);
        treeMapper.update01(tree);
        treeMapper.update02(tree);
    }

    /*根据编号/地址/等级/种类分页查询  编号是模糊查询，其他条件通过联级选择器精准选择*/
    @Override
    public List<Tree> queryByKeys(Tree tree) {
        return treeMapper.selectByKeys(tree);
    }

    @Override
    public boolean addLike(Integer id) {
        return treeMapper.addLike(id)>0;
    }

    @Override
    public Integer getLike(Integer id) {
        return treeMapper.getLike(id);
    }

    @Override
    public List<Tree> selectHotTreeTopTen() {
        return treeMapper.selectHotTreeTopTen();
    }

    @Override
    public boolean addVisitById(Integer id) {
        return treeMapper.addVisit(id)>0;
    }

    @Override
    public Integer queryVisitsById(Integer id) {
        return treeMapper.queryVisitsById(id);
    }

    @Override
    public List<Tree> getTreeByInput(String q) {
        return treeMapper.getTreeByInput(q);
    }

    private void tree_cd(Tree tree) {//对地址进行抽取city,district
        if(tree.getLocation()!=null){
            int s1 = tree.getLocation().indexOf("省");
            int s2 = tree.getLocation().indexOf("市");
            int s3 = tree.getLocation().indexOf("区");
            int s4 = tree.getLocation().indexOf("县");
            int secondss = tree.getLocation().substring(s2 + 1, 10).indexOf("市") + s2 + 1;
            tree.setCity(tree.getLocation().substring(s1 + 1, s2 + 1));//市:
            tree.setDistrict("");//区（县）,县级市
            if (s2 == -1 && s3 == -1 && s4 == -1) {
                tree.setDistrict(null);
            } else if (s4 != -1 && s3 == -1 && s4 < 15) {
                tree.setDistrict(tree.getLocation().substring(s2 + 1, s4 + 1));
            } else if (s4 == -1 && s3 != -1 && s3 < 15) {
                tree.setDistrict(tree.getLocation().substring(s2 + 1, s3 + 1)) ;
            } else if (secondss != s2 && secondss < 15) {
                tree.setDistrict(tree.getLocation().substring(s2 + 1, secondss + 1));
            } else {
                tree.setDistrict(null);
            }
        }
    }

    //合并两个表的对象数据
    private Tree merge(Tree tree01, Tree tree02) {
        tree02.setId(tree01.getId());
        tree02.setCode(tree01.getCode());
        tree02.setTreeType(tree01.getTreeType());
        tree02.setLevel(tree01.getLevel());
        tree02.setAge(tree01.getAge());
        tree02.setFamily(tree01.getFamily());
        tree02.setGenus(tree01.getGenus());
        tree02.setLocation(tree01.getLocation());
        tree02.setCity(tree01.getCity());
        tree02.setDistrict(tree01.getDistrict());
        tree02.setLon(tree01.getLon());
        tree02.setLat(tree01.getLat());
        tree02.setImgFilePath(tree01.getImgFilePath());
        return tree02;
    }
}
